--
-- SwitchView.lua
-- Created by IntelliJ IDEA.
-- User: ema
-- Date: 2018/12/4
--

local SwitchView = MClass("SwitchView")
local SpecType = require("app.public.dao.SpecType")
local string_format = string.format

function SwitchView:ctor()
    self:setVisible(true)

    -- hero
    self._panelHero = self:findChild("Panel/Panel_hero")
    self.changeHeroButton = self:findChild("Panel/Panel_hero/Button")
    self.heroIcon = self:findChild("Panel/Panel_hero/icon")
    self.heroName = self:findChild("Panel/Panel_hero/name")
    self.changeHeroTips = self:findChild("Panel/Panel_hero/tips")

    -- build
    self._panelSkill = self:findChild("Panel/Panel_skill")
    self.changeBuildButton = self:findChild("Panel/Panel_skill/Button")
    self.buildIcon = self:findChild("Panel/Panel_skill/icon")
    self.buildName = self:findChild("Panel/Panel_skill/name")
    self.changeBuildTips = self:findChild("Panel/Panel_skill/tips")

    self:init()
end

function SwitchView:init()
    local function changeHero(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.TRAINING_BATTLE_ESCAPE)
            BattleManager:reqTrainingEscape()
        end
    end
    self.changeHeroButton:addTouchEventListener(changeHero)

    local function changeBuild(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_ESCAPE)
            UIHandler:handleMessage({id = Constant.FUNC_TYPE_SKILL, mode = "open"})
            BattleManager:setNeedReEnterTraining(true)
        end
    end
    self.changeBuildButton:addTouchEventListener(changeBuild)

    self:switchModel(BattleManager:getLastTrainingModel())
end

function SwitchView:switchModel(type)
    self._type = type
    if type == Enums.trainingType.rolePlay then
        self._panelHero:setVisible(true)
        self._panelSkill:setVisible(false)
        self:setRolePlay()
    else
        self._panelHero:setVisible(false)
        self._panelSkill:setVisible(true)
        self:setBuild()
    end
end

function SwitchView:setBuild()
    local specType = SpecType.new(UD:getCurSpecTypeId())
    self.buildIcon:loadTexture(specType:getIcon())
    self.buildName:setString(specType:getNameDes())
    self.changeBuildTips:setString(specType:getShowDutyDes())
end

function SwitchView:setRolePlay()
    if self._type ~= 1 then return end
    local id = BattleManager:getTrainingRolePlayId()
    local confs = GD:queryCosplayConf(id)
    self.heroName:setString(L(confs.name))
    if confs.rolePlayPoolName then
        self.changeHeroTips:setString(L(confs.rolePlayPoolName))
    end
    self.heroIcon:loadTexture(string_format(Res.cosplayIconPath, confs.head))
end

function SwitchView:clear()
    self:setVisible(false)
end

return SwitchView